from flask import Flask
from flask import url_for, redirect, make_response, abort, g, flash, render_template, session
import config
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config.from_object(config)
# g.db=SQLAlchemy.create_engine(config.DB_URI)
db = SQLAlchemy(app)
print(config.SQLALCHEMY_DATABASE_URI)


class Article(db.Model):
    __tablename__ = 'article'
    id = db.Column(db.Integer, nullable=False, autoincrement=True, primary_key=True)
    name = db.Column(db.String(100), nullable=False)
    age = db.Column(db.Integer, nullable=False, default=18)
    addr = db.Column(db.Text, nullable=False)

    def __repr__(self):
        return '<Article %r>' % self.name


db.create_all()


@app.route('/')
def hello_world():
    article = Article(name='刘世培', age=18, addr='四川成都')
    db.session.add(article)
    db.session.commit()
    return 'Hello 中文World!'


@app.route('/query')
def select_article():
    articles = Article.query.all()
    print(articles)
    return render_template('select_article.html',articles=articles)


@app.route('/error/<string:error>')
def no_page(error=None):
    res = make_response("<title>{}</title><h3>No Page {}</h3>".format(error, error), 404)
    res.headers['head'] = "lsp"
    return res


@app.errorhandler(404)
def not_found_page(error):
    # return redirect(url_for('no_page',error=404))
    return "<title>{}</title><h3>{}</h3>".format("No Page", error)


if __name__ == '__main__':
    app.run(debug=True)
